/**
* @version v 1.1.1 2012/03/18
* @package Nerv Widgets, nw.js
* @subpackage Nerv Tab, tab.js
* @author Manuele Castellan <manuele.castellan@gmail.com>
*
* @copyright    Copyright (C) Copyright (c) 2009 Manuele Castellan. All rights reserved.
* @license        GNU/GPLv3, see LICENSE
*
*  This program is free software: you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation, either version 3 of the License, or
*  (at your option) any later version.
*
*  This program is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*
*/

NW3.tabs = {
	Page : function(){
		oPage = document.createElement('div');
		
		// creo oggetto
		var obj = {
			object : oPage,		// oggetto
			visible : true,		// visibile (hide/show)
			contents : null,
			align : 'top',
			classprefix : 'NWTabsPage',
			ajaxUrl : null
		};
		
		obj.show = function(){
			obj.object.style.display = 'block';
			obj.visible = true;
			
			if(obj.ajaxUrl){
				oAjax = new NW3.ajax.Request(obj.ajaxUrl, {
					method : 'GET',
					onComplete : function(response){
						obj.add(response.responseText);
					}
				});
			}
		};
		
		obj.hide = function(){
			obj.object.style.display = 'none';
			obj.visible = false;
		};
		
		obj.loadAjaxUrl = function(url){
			if(url) obj.ajaxUrl = url;
		};
		
		obj.add = function(oo){
			switch (typeof(oo)) {
				case 'object':
					if(oo.nodeType == 1){
						obj.object.appendChild(oo);
					}
					else{
						obj.object.appendChild(oo.output());
					}
					break;
				case 'string':
					obj.object.innerHTML = oo;
			}
		};
		
		obj.setClass = function(val){
			if(val) obj.classprefix = val;
			obj.object.className = obj.classprefix + obj.align;
		};
		
		// setta height
		obj.setHeight = function(val){
			if(val){
				obj.height = parseInt(val);
				obj.object.style.height = obj.height + 'px';
			}
		};
		
		obj.setAlign = function(val){
			if(val.toLowerCase() == 'top'){
				obj.align = 'Top';
				obj.setClass();
			}
			else if(val.toLowerCase() == 'bottom'){
				obj.align = 'Bottom';
				obj.setClass();
			}
		};
		
		obj.output = function(){
			obj.setClass();
			
			return obj.object;
		};
		
		return obj;
	},
	
	Button : function(){
		oButton = new NW3.form.Button();
		//oButton.setClass('tFormButton');
		
		return oButton;
	},
	
	Tabs : function(params){
		params = (!NW3.core.object.isObject(params)) ? null : params;
		
		oContainer = document.createElement('div');
		// oggetto tabs
		this.oTabs = document.createElement('span');
		
		// creo oggetto
		var obj = {
			object : oContainer,	// container
			el : this.oTabs,   		// oggetto
			buttons : null,    		// array bottoni
			active : 0,    		// bottone premuto/attivo di default
			width : null,			// tab width
			height : null,			// tab height
			pages : null,    		// array pages
			
			classprefix : 'NWTabs'
		};
		
		obj.newTab = function(options){
			if(options.name){
				oButton = new NW3.tabs.Button();
				oButton.setText(options.name);
				
				oPage = new NW3.tabs.Page();
				if(options.html){
					if(NW3.core.object.isObject(options.html)) options.html = NW3.core.object.setOuterHTML(options.html);
					oPage.add(options.html);
				}
				
				if(obj.height) oPage.setHeight(obj.height);
				
				if(options.action){
					oButton.onMouseUp(function(){
						options.action();
					});
				}
				
				if(options.ajaxUrl) oPage.loadAjaxUrl(options.ajaxUrl);
				
				obj.add(oButton, oPage);
			}
		};
		
		// -----
		
		// creo array bottoni
		obj.buttons = new Array();
		// array pagine
		obj.pages = new Array();
		
		// setta width della tab
		obj.setWidth = function(val){
			if(val){
				obj.width = parseInt(val);
				obj.object.style.width = obj.width + 'px';
			}
		};
		
		// setta height della tab
		obj.setHeight = function(val){
			if(val){
				obj.height = parseInt(val);
				obj.object.style.height = obj.height + 'px';
			}
		};
		
		// aggiorna stato pulsanti tabs
		obj.update = function(id){
			for(i=0; i<obj.buttons.length; i++){
				if(obj.buttons[i].id == id){
					obj.buttons[i].setStatus('pressed');
					if(obj.pages[i]) obj.pages[i].show();
				}
				else{
					obj.buttons[i].setStatus('enabled');
					if(obj.pages[i]) obj.pages[i].hide();
				}
			}
		};
		
		// setta bottone premuto
		obj.setPressed = function(val){
			if(val > 0){
				obj.active = (val-1);
			}
		};
		
		// buttons counter
		bCounter = 0;
		
		// add buttons
		obj.add = function(oBut, oPage){
			oBut.setId(bCounter);
			oBut.onMouseDown(function(id){
				obj.update(id);
			});
			obj.buttons.push(oBut);
			
			if(oPage) obj.pages.push(oPage);
			
			bCounter++;
		};
		
		// set class of buttons
		obj.setButtonsClass = function(){
			for(b=0; obj.buttons.length>b; b++){
				//alert(obj.classprefix + obj.align + 'Button');
				obj.buttons[b].setClass(obj.classprefix + obj.align + 'Button');
			}
		};
		
		// set align
		obj.setAlign = function(align){
			switch (align) {
				case 'bottom':
					obj.align = 'Bottom';
					break;
				case 'top':
				default:
					obj.align = 'Top';
					break;
			}
		};
		
		// get align
		obj.getAlign = function(){
			if(obj.align) return obj.align.toLocaleLowerCase();
		};
		
		// inserisco la tabs
		obj.insertTabs = function(){
			for(r=0; r<obj.buttons.length; r++){
				if(obj.buttons[r].id == obj.active){
					obj.update(obj.buttons[r].id);
				}
				
				obj.el.appendChild(obj.buttons[r].output());
			}
			obj.object.appendChild(obj.el);
		};
		
		// inserisco le pages 
		obj.insertPages = function(){
			for(r=0; r<obj.pages.length; r++){
				obj.pages[r].setAlign(obj.align);
				obj.object.appendChild(obj.pages[r].output());
			}
		};
		
		obj.output = function(){
			// setto deafault align top
			if(!obj.align) obj.setAlign('top');
			var classname = obj.classprefix + obj.align;
			obj.el.className = classname;
			
			obj.object.className = obj.classprefix;
			
			obj.setButtonsClass();
			
			if(obj.getAlign() == 'bottom'){
				obj.insertPages();
				obj.insertTabs();
			}
			else{
				obj.insertTabs();
				obj.insertPages();
			}
			
			return obj.object;
		};
		
		obj.print = function(){
			NW3.core.object.print(obj.output());
		};
		
		obj.replace = function(targetElement){
			NW3.core.object.replace(targetElement, obj.output());
		};
		
		obj.insertAfter = function(targetElement){
			NW3.core.object.insertAfter(obj.output(), targetElement);
		};
		
		obj.insertBefore = function(targetElement){
			NW3.core.object.insertBefore(obj.output(), targetElement);
		};
		
		obj.appendChild = function(targetElement){
			NW3.core.object.appendChild(obj.output(), targetElement);
		};
		
		// set options
		if(params){
			if(params.width) obj.setWidth(params.width);
			if(params.height) obj.setHeight(params.height);
			if(params.align) obj.setAlign(params.align);
			if(params.pressed) obj.setPressed(params.pressed);
		}
		
		return obj;
	}
};